package com.jyun.web;

import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/login")
public class LoginController {
    OkHttpClient client = new OkHttpClient();

    @RequestMapping("/doLogin")
    @ResponseBody
    public String login(HttpServletRequest request, @RequestParam(name = "uid", defaultValue = "") String name, @RequestParam(name = "pwd", defaultValue = "") String password, HttpServletResponse response) {
        String time = String.valueOf(System.currentTimeMillis());
        RequestBody params = new FormBody.Builder()
                .add("uid", name)
                .add("pwd", password)
                .add("timestamp", time)
                .build();
        Request login = new Request.Builder()
                .url("https://coyee.com/action/user/signin")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Referer", "https://coyee.com/signin")
                .post(params)
                .build();
        Response result = null;
        try {
            result = client.newCall(login).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (result.isSuccessful()) {
            try {
                List<String> cookieStr = result.headers("Set-Cookie");
                String cookie = String.valueOf(cookieStr);
                System.out.println(cookie);
                request.getSession(true).setAttribute("cookie", cookie);
                System.out.println(result.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSON.toJSONString(com.jyun.entity.Response.ERROR);

    }

    @RequestMapping("/zi")
    @ResponseBody
    public String verifyCode(HttpServletRequest request) {

        String[] emoji = {":bowtie:", ":smile:", ":laughing:", ":blush:", ":smiley:", ":relaxed:", ":smirk:", ":heart_eyes:", ":kissing_heart:", ":kissing_closed_eyes:", ":flushed:", ":relieved:", ":satisfied:", ":grin:", ":wink:", ":stuck_out_tongue_winking_eye:", ":stuck_out_tongue_closed_eyes:", ":grinning:", ":kissing:", ":kissing_smiling_eyes:", ":stuck_out_tongue:", ":sleeping:", ":worried:", ":frowning:", ":anguished:", ":open_mouth:", ":grimacing:", ":confused:", ":hushed:", ":expressionless:", ":unamused:", ":sweat_smile:", ":sweat:", ":weary:", ":pensive:", ":disappointed:", ":confounded:", ":fearful:", ":cold_sweat:", ":persevere:", ":cry:", ":sob:", ":joy:", ":astonished:", ":scream:", ":tired_face:", ":angry:", ":rage:", ":triumph:", ":sleepy:", ":yum:", ":mask:", ":sunglasses:", ":dizzy_face:", ":imp:", ":smiling_imp:", ":neutral_face:", ":innocent:", ":alien:", ":heart:", ":cupid:", ":sparkles:", ":boom:", ":exclamation:", ":question:", ":zzz:", ":thumbsup:", ":-1:", ":ok_hand:", ":punch:", ":fist:", ":v:", ":open_hands:", ":point_up:", ":point_down:", ":point_left:", ":point_right:", ":raised_hands:", ":pray:", ":clap:", ":muscle:", ":metal:", ":kiss:", ":trollface:"};
        //创建HTTP请求,吱~
        long t = System.currentTimeMillis();
        Random random = new Random(t);
        String time = String.valueOf(t);
        RequestBody params = new FormBody.Builder()
                .add("user", "12047")
                .add("userCode", "0mJL92NHE1Fpg3ll1ejpIYhycwV15axMzkoTDAsx")
                .add("timestamp", time)
                .add("memo", emoji[random.nextInt(84)])
                .build();
        Request login = new Request.Builder()
                .url("https://coyee.com/action/tweet/add")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Referer", "https://coyee.com/zhi")
                .addHeader("Cookie", (String) request.getSession().getAttribute("cookie"))
                .post(params)
                .build();
        Response result = null;
        try {
            result = client.newCall(login).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (result.isSuccessful()) {
            try {
                System.out.println(result.body().string());

                return JSON.toJSONString(com.jyun.entity.Response.SUCCESS);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSON.toJSONString(com.jyun.entity.Response.ERROR);
    }
}
